<?php


namespace app\dao\query;


use app\model\Roles;
use app\model\Routes;
use app\model\User;

class RoutesQueryDao
{
    private $query;

    public function __construct()
    {
        $this->query = new Routes();
    }

    /**
     * 获取列表
     * @param int $page -
     * @param int $limit -
     * @return \think\Paginator -
     */
    public function getList(int $page = 1, int $limit = 10)
    {
        return $this->query->paginate([
            'list_rows' => $limit,
            'page' => $page
        ]);
    }

    /**
     * 获取所有数据
     * @return array -
     */
    public function getAll()
    {
        return $this->query->select();
    }

    /**
     * 获取一个
     * @return mixed
     */
    public function getOne()
    {
        return $this->query->find();
    }


    /**
     * 获取当前筛选下的总记录数
     * @return int -
     */
    public function getCount()
    {
        return $this->query->count();
    }

    /**
     * 根据url查询
     * @param String $url
     * @return $this
     */
    public function filterByUrl(String $url) {
        if ($url != null){
            $this->query = $this->query->where('url', $url);
        }
        return $this;
    }

    public function filterById(int $id) {
        if ($id != null){
            $this->query = $this->query->where('id', $id);
        }
        return $this;
    }

    /**
     * 根据method查询
     * @param String $method
     * @return $this
     */
    public function filterByMethod(String $method) {
        if ($method != null){
            $this->query = $this->query->where('method', $method);
        }
        return $this;
    }

    public function fuzzyByUrl(String $url){
        if ($url != null){
            $this->query = $this->query->whereLike('url', '%'.$url.'%');
        }
        return $this;
    }

    public function order($key, $asc = true){
        if ($key != null){
            $this->query = $this->query->order($key, $asc? 'asc' : 'desc');
        }
        return $this;
    }

    public function withRole(){
        $this->query = $this->query->with(['role'=>function($query){
            $query->with(['rolename']);
        }]);
        return $this;
    }


}